//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "LocalConstantVol.h"
using namespace Cephei::QL::Termstructures::Volatility::Equityfx;
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Quote.h>
#include <gen/QL/Times/Period.h>
#include <gen/QL/Termstructures/Volatility/Equityfx/LocalVolTermStructure.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Volatility::Equityfx::CLocalConstantVol::CLocalConstantVol (UInt32 settlementDays, Cephei::QL::Times::ICalendar^ prm1, Cephei::QL::IQuote^ volatility, Cephei::QL::Times::IDayCounter^ dayCounter) : CLocalVolTermStructure(CLocalConstantVol::typeid)
{
    CCalendar^ _Cprm1;
    CQuote^ _Cvolatility;
    CDayCounter^ _CdayCounter;
    try
    {
#ifdef HANDLE
        _phLocalConstantVol = NULL;
#endif
        QuantLib::Natural _settlementDays = (QuantLib::Natural)ValueHelper::Convert (settlementDays); //d
        _Cprm1 = safe_cast<CCalendar^> (prm1);
        _Cprm1->Lock();
        QuantLib::Calendar& _prm1 = static_cast<QuantLib::Calendar&> (_Cprm1->GetReference ()); 
        _Cvolatility = safe_cast<CQuote^> (volatility);
        _Cvolatility->Lock();
        Handle<QuantLib::Quote>& _volatility = static_cast<Handle<QuantLib::Quote>&> (_Cvolatility->GetHandle ()); 
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
        _ppLocalConstantVol = new boost::shared_ptr<QuantLib::LocalConstantVol> (new QuantLib::LocalConstantVol ( _settlementDays,  _prm1,  _volatility,  _dayCounter ));
        SetLocalVolTermStructure (boost::dynamic_pointer_cast<QuantLib::LocalVolTermStructure> (*_ppLocalConstantVol));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprm1 != nullptr) _Cprm1->Unlock();
        if (_Cvolatility != nullptr) _Cvolatility->Unlock();
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
    }
}
Cephei::QL::Termstructures::Volatility::Equityfx::CLocalConstantVol::CLocalConstantVol (UInt32 settlementDays, Cephei::QL::Times::ICalendar^ prm1, Double volatility, Cephei::QL::Times::IDayCounter^ dayCounter) : CLocalVolTermStructure(CLocalConstantVol::typeid)
{
    CCalendar^ _Cprm1;
    CDayCounter^ _CdayCounter;
    try
    {
#ifdef HANDLE
        _phLocalConstantVol = NULL;
#endif
        QuantLib::Natural _settlementDays = (QuantLib::Natural)ValueHelper::Convert (settlementDays); //d
        _Cprm1 = safe_cast<CCalendar^> (prm1);
        _Cprm1->Lock();
        QuantLib::Calendar& _prm1 = static_cast<QuantLib::Calendar&> (_Cprm1->GetReference ()); 
        QuantLib::Volatility _volatility = (QuantLib::Volatility)ValueHelper::Convert (volatility); //d
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
        _ppLocalConstantVol = new boost::shared_ptr<QuantLib::LocalConstantVol> (new QuantLib::LocalConstantVol ( _settlementDays,  _prm1,  _volatility,  _dayCounter ));
        SetLocalVolTermStructure (boost::dynamic_pointer_cast<QuantLib::LocalVolTermStructure> (*_ppLocalConstantVol));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprm1 != nullptr) _Cprm1->Unlock();
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
    }
}
Cephei::QL::Termstructures::Volatility::Equityfx::CLocalConstantVol::CLocalConstantVol (DateTime referenceDate, Cephei::QL::IQuote^ volatility, Cephei::QL::Times::IDayCounter^ dayCounter) : CLocalVolTermStructure(CLocalConstantVol::typeid)
{
    CQuote^ _Cvolatility;
    CDayCounter^ _CdayCounter;
    try
    {
#ifdef HANDLE
        _phLocalConstantVol = NULL;
#endif
        QuantLib::Date _referenceDate = (QuantLib::Date)ValueHelper::Convert (referenceDate); //d
        _Cvolatility = safe_cast<CQuote^> (volatility);
        _Cvolatility->Lock();
        Handle<QuantLib::Quote>& _volatility = static_cast<Handle<QuantLib::Quote>&> (_Cvolatility->GetHandle ()); 
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
        _ppLocalConstantVol = new boost::shared_ptr<QuantLib::LocalConstantVol> (new QuantLib::LocalConstantVol ( _referenceDate,  _volatility,  _dayCounter ));
        SetLocalVolTermStructure (boost::dynamic_pointer_cast<QuantLib::LocalVolTermStructure> (*_ppLocalConstantVol));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cvolatility != nullptr) _Cvolatility->Unlock();
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
    }
}
Cephei::QL::Termstructures::Volatility::Equityfx::CLocalConstantVol::CLocalConstantVol (DateTime referenceDate, Double volatility, Cephei::QL::Times::IDayCounter^ dayCounter) : CLocalVolTermStructure(CLocalConstantVol::typeid)
{
    CDayCounter^ _CdayCounter;
    try
    {
#ifdef HANDLE
        _phLocalConstantVol = NULL;
#endif
        QuantLib::Date _referenceDate = (QuantLib::Date)ValueHelper::Convert (referenceDate); //d
        QuantLib::Volatility _volatility = (QuantLib::Volatility)ValueHelper::Convert (volatility); //d
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
        _ppLocalConstantVol = new boost::shared_ptr<QuantLib::LocalConstantVol> (new QuantLib::LocalConstantVol ( _referenceDate,  _volatility,  _dayCounter ));
        SetLocalVolTermStructure (boost::dynamic_pointer_cast<QuantLib::LocalVolTermStructure> (*_ppLocalConstantVol));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
    }
}
Cephei::QL::Termstructures::Volatility::Equityfx::CLocalConstantVol::CLocalConstantVol (boost::shared_ptr<QuantLib::LocalConstantVol>& childNative, Object^ owner) : CLocalVolTermStructure(CLocalConstantVol::typeid)
{
#ifdef HANDLE
	_phLocalConstantVol = NULL;
#endif
	_ppLocalConstantVol = &childNative;
    _ppLocalVolTermStructure = new boost::shared_ptr<QuantLib::LocalVolTermStructure> (boost::dynamic_pointer_cast<QuantLib::LocalVolTermStructure> (*_ppLocalConstantVol));
}
Cephei::QL::Termstructures::Volatility::Equityfx::CLocalConstantVol::CLocalConstantVol (QuantLib::LocalConstantVol& childNative, Object^ owner) : CLocalVolTermStructure(CLocalConstantVol::typeid)
{
#ifdef HANDLE
	_phLocalConstantVol = NULL;
#endif
	_ppLocalConstantVol = new boost::shared_ptr<QuantLib::LocalConstantVol> (&childNative);
    _ppLocalVolTermStructure = new boost::shared_ptr<QuantLib::LocalVolTermStructure> (boost::dynamic_pointer_cast<QuantLib::LocalVolTermStructure> (*_ppLocalConstantVol));
    _LocalConstantVolOwner = owner;
    _LocalVolTermStructureOwner = owner;
}

Cephei::QL::Termstructures::Volatility::Equityfx::CLocalConstantVol::CLocalConstantVol (CLocalConstantVol^ copy) : CLocalVolTermStructure(CLocalConstantVol::typeid)
{
#ifdef HANDLE
	_phLocalConstantVol = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppLocalConstantVol = new boost::shared_ptr<QuantLib::LocalConstantVol> (copy->GetShared());
        _ppLocalVolTermStructure = new boost::shared_ptr<QuantLib::LocalVolTermStructure> (boost::dynamic_pointer_cast<QuantLib::LocalVolTermStructure> (*_ppLocalConstantVol));
    }
}
Cephei::QL::Termstructures::Volatility::Equityfx::CLocalConstantVol::CLocalConstantVol (PLATFORM::Type^ t) : CLocalVolTermStructure(CLocalConstantVol::typeid)
{
#ifdef HANDLE
	_phLocalConstantVol = NULL;
#endif
	if (!t->IsSubclassOf(CLocalConstantVol::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Volatility::Equityfx::CLocalConstantVol::CLocalConstantVol (QuantLib::Handle<QuantLib::LocalConstantVol>& childNative, Object^ owner)  : CLocalVolTermStructure(CLocalConstantVol::typeid)
{
	_phLocalConstantVol = &childNative;
	_ppLocalConstantVol = &static_cast<boost::shared_ptr<QuantLib::LocalConstantVol>>(childNative.currentLink());
    _ppLocalVolTermStructure = new boost::shared_ptr<QuantLib::LocalVolTermStructure> (boost::dynamic_pointer_cast<QuantLib::LocalVolTermStructure> (*_ppLocalConstantVol));
    _LocalConstantVolOwner = owner;
}
Cephei::QL::Termstructures::Volatility::Equityfx::CLocalConstantVol::CLocalConstantVol (QuantLib::Handle<QuantLib::LocalConstantVol> childNative)  : CLocalVolTermStructure(CLocalConstantVol::typeid)
{
	_phLocalConstantVol = &childNative;
	_ppLocalConstantVol = &static_cast<boost::shared_ptr<QuantLib::LocalConstantVol>>(childNative.currentLink());
    _ppLocalVolTermStructure = new boost::shared_ptr<QuantLib::LocalVolTermStructure> (boost::dynamic_pointer_cast<QuantLib::LocalVolTermStructure> (*_ppLocalConstantVol));
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Volatility::Equityfx::CLocalConstantVol::CLocalConstantVol (QuantLib::LocalConstantVol childNative)  : CLocalVolTermStructure(CLocalConstantVol::typeid)
{
#ifdef HANDLE
	_phLocalConstantVol = NULL;
#endif
	_ppLocalConstantVol = new boost::shared_ptr<QuantLib::LocalConstantVol> (new QuantLib::LocalConstantVol (childNative));
    _ppLocalVolTermStructure = new boost::shared_ptr<QuantLib::LocalVolTermStructure> (boost::dynamic_pointer_cast<QuantLib::LocalVolTermStructure> (*_ppLocalConstantVol));
}
#endif

Cephei::QL::Termstructures::Volatility::Equityfx::CLocalConstantVol::~CLocalConstantVol ()
{
    if (_ppLocalConstantVol != NULL)
    {
	    delete _ppLocalConstantVol;
        _ppLocalConstantVol = NULL;
    }
}
Cephei::QL::Termstructures::Volatility::Equityfx::CLocalConstantVol::!CLocalConstantVol ()
{
    if (_ppLocalConstantVol != NULL)
    {
	    delete _ppLocalConstantVol;
    }
}
QuantLib::LocalConstantVol& Cephei::QL::Termstructures::Volatility::Equityfx::CLocalConstantVol::GetReference ()
{
    if (_ppLocalConstantVol == NULL) throw REFNEW NativeNullException ();
	return **_ppLocalConstantVol;
}
boost::shared_ptr<QuantLib::LocalConstantVol>& Cephei::QL::Termstructures::Volatility::Equityfx::CLocalConstantVol::GetShared ()
{
    if (_ppLocalConstantVol == NULL) throw REFNEW NativeNullException ();
	return *_ppLocalConstantVol;
}
QuantLib::LocalConstantVol* Cephei::QL::Termstructures::Volatility::Equityfx::CLocalConstantVol::GetPointer ()
{
    if (_ppLocalConstantVol == NULL) throw REFNEW NativeNullException ();
	return &**_ppLocalConstantVol;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::LocalConstantVol>& Cephei::QL::Termstructures::Volatility::Equityfx::CLocalConstantVol::GetHandle ()
{
	if (_phLocalConstantVol == NULL)
	{
		_phLocalConstantVol = new Handle<QuantLib::LocalConstantVol> (*_ppLocalConstantVol);
	}
	return *_phLocalConstantVol;
}
#endif
bool Cephei::QL::Termstructures::Volatility::Equityfx::CLocalConstantVol::HasNative () 
{
	return (_ppLocalConstantVol != NULL);
}

Cephei::QL::Times::IDayCounter^ Cephei::QL::Termstructures::Volatility::Equityfx::CLocalConstantVol::DayCounter::get ()
{
    try
    {
    	QuantLib::DayCounter _rv = (QuantLib::DayCounter)(*_ppLocalConstantVol)->dayCounter ( );   
        Cephei::QL::Times::CDayCounter^ _nrv = REFNEW Cephei::QL::Times::CDayCounter (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Termstructures::Volatility::Equityfx::CLocalConstantVol::MaxDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppLocalConstantVol)->maxDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::Equityfx::CLocalConstantVol::MaxStrike::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppLocalConstantVol)->maxStrike ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::Equityfx::CLocalConstantVol::MinStrike::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppLocalConstantVol)->minStrike ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Termstructures::Volatility::Equityfx::ILocalConstantVol^ Cephei::QL::Termstructures::Volatility::Equityfx::CLocalConstantVol_Factory::Create (UInt32 settlementDays, Cephei::QL::Times::ICalendar^ prm1, Cephei::QL::IQuote^ volatility, Cephei::QL::Times::IDayCounter^ dayCounter)
{
    return REFNEW CLocalConstantVol ( settlementDays,  prm1,  volatility,  dayCounter);
}
Cephei::QL::Termstructures::Volatility::Equityfx::ILocalConstantVol^ Cephei::QL::Termstructures::Volatility::Equityfx::CLocalConstantVol_Factory::Create (UInt32 settlementDays, Cephei::QL::Times::ICalendar^ prm1, Double volatility, Cephei::QL::Times::IDayCounter^ dayCounter)
{
    return REFNEW CLocalConstantVol ( settlementDays,  prm1,  volatility,  dayCounter);
}
Cephei::QL::Termstructures::Volatility::Equityfx::ILocalConstantVol^ Cephei::QL::Termstructures::Volatility::Equityfx::CLocalConstantVol_Factory::Create (DateTime referenceDate, Cephei::QL::IQuote^ volatility, Cephei::QL::Times::IDayCounter^ dayCounter)
{
    return REFNEW CLocalConstantVol ( referenceDate,  volatility,  dayCounter);
}
Cephei::QL::Termstructures::Volatility::Equityfx::ILocalConstantVol^ Cephei::QL::Termstructures::Volatility::Equityfx::CLocalConstantVol_Factory::Create (DateTime referenceDate, Double volatility, Cephei::QL::Times::IDayCounter^ dayCounter)
{
    return REFNEW CLocalConstantVol ( referenceDate,  volatility,  dayCounter);
}
